package com.xuanyuanxinyu.view.adapter

import android.content.Context
import android.view.View
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.xuanyuan.ui.adapter.BaseListAdapter
import com.xuanyuan.ui.adapter.BaseListAdapter.MultipleType
import com.xuanyuanxinyu.utils.livebus.LiveStringBean
import com.xuanyuanxinyu.view.R

/**
 * @FileName:
 * @Description: Adapter的使用案例
 * @author ken.luo
 * @date 2023/4/4
 * @version V1.0
 * @email 424533553@qq.com
 */
class AdapterDemo {
    var adapter : BaseListAdapter<LiveStringBean>? = null
    
    fun notifyAdapter() {
        val list = arrayListOf<LiveStringBean>()
        val bean = LiveStringBean()
        bean.value = false
        bean.msg = "Test Print1"
        list.add(bean)
        val bean2 = LiveStringBean()
        bean2.value = true
        bean2.msg = "Test Print2"
        list.add(bean2)
        val bean3 = LiveStringBean()
        bean3.value = true
        bean3.msg = "Test Print1"
        list.add(bean3)
        val bean4 = LiveStringBean()
        bean4.value = false
        bean4.msg = "Test Print1"
        list.add(bean4)
        
        adapter?.submitList(list)
    }
    
    fun initAdapter(rcList : RecyclerView,context : Context) {
        val multiType = object : MultipleType<LiveStringBean> {
            override fun getTypeLayoutId(itemType : Int) : Int {
                return if (BaseListAdapter.ItemType.ITEM_TYPE_EMPTY == itemType) {
                    R.layout.item_empty
                } else {
                    R.layout.item_normal
                }
            }
            
            override fun getItemType(position : Int,t : LiveStringBean) : Int {
                return if (t.value) {
                    BaseListAdapter.ItemType.ITEM_TYPE_EMPTY
                } else {
                    BaseListAdapter.ItemType.ITEM_TYPE_NORMAL
                }
            }
        }
        val diffUtil = object : DiffUtil.ItemCallback<LiveStringBean>() {
            override fun areItemsTheSame(oldItem : LiveStringBean,newItem : LiveStringBean) : Boolean {
                return false
            }
            
            override fun areContentsTheSame(oldItem : LiveStringBean,newItem : LiveStringBean) : Boolean {
                return false
            }
        }
        adapter = object : BaseListAdapter<LiveStringBean>(multiType,diffUtil) {
            override fun bindUi(itemView : View,t : LiveStringBean,viewType : Int) {
            
            }
        }
        val linearLayoutManager = LinearLayoutManager(context)
        linearLayoutManager.orientation = LinearLayoutManager.VERTICAL
        rcList.setLayoutManager(linearLayoutManager)
        rcList.adapter = adapter
    }
    
}